//
//  ToastActivity.m
//  ToastActivity
//
//  Created by Johnny on 15/9/8.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "ToastActivity.h"

static NSInteger const kOnlyToastActivityTag        =  99999;

static CGFloat   const kToastActivityWidth          =  100.0;
static CGFloat   const kToastActivityMaxWidth       =  160.0;
//static CGFloat   const kToastActivityHeight         =  120.0;
static CGFloat   const KCircleWidth                 =  60.0;
static CGFloat   const KLogoWidth                   =  50.0;


@interface ToastActivity ()

@end

@implementation ToastActivity

//旋转动画
- (CABasicAnimation *)addAnimation {
    CABasicAnimation *rotate =  [CABasicAnimation animationWithKeyPath: @"transform.rotation.z"];
    rotate.removedOnCompletion = FALSE;
    rotate.fillMode = kCAFillModeForwards;
    [rotate setToValue: [NSNumber numberWithFloat: M_PI * 2]];
    rotate.repeatCount = HUGE_VALF;
    rotate.duration = 0.8;
    rotate.cumulative = TRUE;
    rotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    return rotate;
}

- (id)initWithText:(NSString *)text {
    self = [self initWithFrame:CGRectZero];
    if (self) {
        _circleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KCircleWidth, KCircleWidth)];
        _circleImageView.image = [UIImage imageNamed:@"activity_circle"];
        [_circleImageView.layer addAnimation:[self addAnimation] forKey:@"rotateAnimation"];
        [self addSubview:_circleImageView];
        
        _logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KLogoWidth, KLogoWidth)];
        _logoImageView.image = [UIImage imageNamed:@"activity_logo"];
        [self addSubview:_logoImageView];
        
        _textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.font = [UIFont systemFontOfSize:15];
        _textLabel.textColor = [UIColor whiteColor];
        _textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _textLabel.numberOfLines = 0;
        [self addSubview:_textLabel];
        [self sizeToFitText:text];
    }
    return self;
}

- (void)sizeToFitText:(NSString *)text {
    _textLabel.text = text;
    float width = 0;
//    CGSize size = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil]];
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:15]];
    if (size.width < kToastActivityWidth) {
        width = kToastActivityWidth;
    } else if (size.width < kToastActivityMaxWidth) {
        width = size.width;
    } else {
        width = kToastActivityMaxWidth;
    }
    CGSize size1 = [text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping];
    self.frame = CGRectMake(0, 0, width + 20, size1.height + KCircleWidth + 30);
    _circleImageView.frame = CGRectMake((self.frame.size.width - KCircleWidth)/2.0, 10, KCircleWidth, KCircleWidth);
    _logoImageView.frame = CGRectMake((self.frame.size.width - KLogoWidth)/2.0, 15, KLogoWidth, KLogoWidth);
    _textLabel.frame = CGRectMake((self.frame.size.width - size1.width)/2.0, KCircleWidth + 20, size1.width, size1.height);

//    CGRect frame = [text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
//    self.frame = CGRectMake(0, 0, width + 20, frame.size.height + KCircleWidth + 30);
//    _circleImageView.frame = CGRectMake((self.frame.size.width - KCircleWidth)/2.0, 10, KCircleWidth, KCircleWidth);
//    _logoImageView.frame = CGRectMake((self.frame.size.width - KLogoWidth)/2.0, 15, KLogoWidth, KLogoWidth);
//    _textLabel.frame = CGRectMake((self.frame.size.width - frame.size.width)/2.0, KCircleWidth + 20, frame.size.width, frame.size.height);
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = 8;
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    }
    return self;
}

+ (void)makeActivity:(NSString *)text InView:(UIView *)view {
    if ([view viewWithTag:kOnlyToastActivityTag]) {
        return;
    }
    ToastActivity *activity = [[ToastActivity alloc] initWithText:text];
    activity.tag = kOnlyToastActivityTag;
    CGPoint point = CGPointMake(view.frame.size.width/2.0, view.frame.size.height/2.0);
    activity.center = point;
    [view addSubview:activity];
}

+ (void)makeSuccess:(NSString *)text InView:(UIView *)view {
    ToastActivity *activity = [ToastActivity shareInView:view];
    [activity showText:text IsSuccess:YES InView:view];
}

+ (void)makeError:(NSString *)text InView:(UIView *)view {
    ToastActivity *activity = [ToastActivity shareInView:view];
    [activity showText:text IsSuccess:NO InView:view];
}

//
+ (ToastActivity *)shareInView:(UIView *)view {
    if ([view viewWithTag:kOnlyToastActivityTag]) {
        return (ToastActivity *)[view viewWithTag:kOnlyToastActivityTag];
    }
    return nil;
}

- (void)showText:(NSString *)text IsSuccess:(BOOL)isSuccess InView:(UIView *)view {
    [_circleImageView.layer removeAnimationForKey:@"rotateAnimation"];
    [_circleImageView removeFromSuperview];
    if (isSuccess) {
        _logoImageView.image = [UIImage imageNamed:@"success"];
    } else {
        _logoImageView.image = [UIImage imageNamed:@"error"];
    }
    [self sizeToFitText:text];
    CGPoint point = CGPointMake(view.frame.size.width/2.0, view.frame.size.height/2.0);
    self.center = point;
    [self performSelector:@selector(dismiss) withObject:nil afterDelay:1];
}

+ (void)dismissInView:(UIView *)view {
    ToastActivity *activity = [ToastActivity shareInView:view];
    [activity dismiss];
}

- (void)dismiss {
    [self removeFromSuperview];
}

@end
